package fact.view.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.faces.application.FacesMessage;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.icefaces.ace.component.chart.Axis;
import org.icefaces.ace.component.chart.AxisType;
import org.icefaces.ace.model.chart.CartesianSeries;
import org.icefaces.ace.model.chart.CartesianSeries.CartesianType;

import fact.view.DTO.ReportDTO;
import fact.view.model.FactReportModelBean;

@ManagedBean(name= "factReportBean")
//@RequestScoped
@CustomScoped(value = "#{window}")
public class FactReportBean implements Serializable {

	private static final long serialVersionUID = -7617917330249050697L;


	
	private List<ReportDTO> factData;
	 
    @ManagedProperty(value="#{factReportModelBean}")
    private FactReportModelBean factReportModelBean;
    
    @ManagedProperty(value="#{loginBean}")
    private LoginBean loginBean;
    
    ArrayList<ReportDTO> list=new ArrayList<ReportDTO>();
    private int fromMonth;
    private int toMonth;
    private int fromYear;
    private int toYear;
    private String type="csv";

 
    
    private List<CartesianSeries> barData=new ArrayList<CartesianSeries>();
    
    public void getFactReport() 
	{
		System.out.println("In getFactReport method");
		CartesianSeries cs=new CartesianSeries();
		CartesianSeries csActual=new CartesianSeries();
		CartesianSeries csTarget=new CartesianSeries();
		CartesianSeries csMargin=new CartesianSeries();
		
		cs.setType(CartesianType.BAR);
		csActual.setType(CartesianType.LINE);
		csTarget.setType(CartesianType.LINE);
		csMargin.setType(CartesianType.LINE);
		
		
		String monthYearStr=factReportModelBean.createMonthYearStr(fromMonth, fromYear, toMonth, toYear);
		try{
		 list=factReportModelBean.getFactReport(loginBean.getLoginName(),monthYearStr);
		 for(int i=0;i<list.size();i++)
		 {
			 cs.add(list.get(i).getMonthYear(), list.get(i).getTotal());
			 csActual.add(list.get(i).getMonthYear(), list.get(i).getActual());
			 csTarget.add(list.get(i).getMonthYear(), list.get(i).getTarget());
			 csMargin.add(list.get(i).getMonthYear(), list.get(i).getMargin());
		 }
		 cs.setLabel("Month,Year/Total");
		 csActual.setLabel("Month,Year/Actual");
		 csTarget.setLabel("Month,Year/Target");
		 csMargin.setLabel("Month,Year/Margin");
		 
		//http://www.icesoft.org/wiki/display/ICE/Chart
		 
		 barData.add(cs);
		 barData.add(csActual);
		 barData.add(csTarget);
		 barData.add(csMargin);	 
		}
		
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
    
    public void submit(ActionEvent ae)
    {
    	String msg="";
    	FacesContext.getCurrentInstance().renderResponse();
		 FactReportBean myBean = (FactReportBean) FacesContext.getCurrentInstance().getELContext().getELResolver().getValue(FacesContext.getCurrentInstance().getELContext(), null, "factReportBean");
		 myBean.getBarData().clear();
		 
    	if(fromMonth==99 || toMonth==99)
    		msg="Please select Month correctly.";
    	else if(fromYear==9999 || toYear==9999)
    		msg="Please select Year correctly.";
    	else if(fromYear>toYear)
    		msg="From Year cannot be greater than To Year.";
    	else if(fromYear==toYear && fromMonth>toMonth)
    		msg="From month cannot greater than To Month.";
    	
    	if(!msg.equalsIgnoreCase(""))
    	{
    	FacesMessage facesMessage=new FacesMessage(msg);
    	FacesContext facesContext=FacesContext.getCurrentInstance();
    	String clientId=null;
    	facesContext.addMessage(clientId,facesMessage);
    	}
    }
    
    
	public List<ReportDTO> getFactData() {
		return factData;
	}

	public void setFactData(List<ReportDTO> factData) {
		this.factData = factData;
	}

	

	public FactReportModelBean getFactReportModelBean() {
		return factReportModelBean;
	}


	public void setFactReportModelBean(FactReportModelBean factReportModelBean) {
		this.factReportModelBean = factReportModelBean;
	}


	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}


	public int getFromMonth() {
		return fromMonth;
	}


	public void setFromMonth(int fromMonth) {
		this.fromMonth = fromMonth;
	}


	public int getToMonth() {
		return toMonth;
	}


	public void setToMonth(int toMonth) {
		this.toMonth = toMonth;
	}


	public int getFromYear() {
		return fromYear;
	}


	public void setFromYear(int fromYear) {
		this.fromYear = fromYear;
	}


	public int getToYear() {
		return toYear;
	}


	public void setToYear(int toYear) {
		this.toYear = toYear;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public ArrayList<ReportDTO> getList() {
		return list;
	}


	public void setList(ArrayList<ReportDTO> list) {
		this.list = list;
	}
    
	private Axis barDemoDefaultAxis = new Axis() {{
        setTickAngle(-30);
    }};

    private Axis barDemoXAxis = new Axis() {{
        setType(AxisType.CATEGORY);
    }};

    private Axis barDemoYAxis = new Axis() {{
            setAutoscale(true);
           // setTickInterval("5");
            setLabel("USD Millions");
}};


    
    public List<CartesianSeries> getBarData() {
        return barData;
    }

    public void setBarData(List<CartesianSeries> barData) {
        this.barData = barData;
    }
    
    public Axis getBarDemoXAxis() {
        return barDemoXAxis;
    }

    public void setBarDemoXAxis(Axis barDemoXAxis) {
        this.barDemoXAxis = barDemoXAxis;
    }

    public Axis getBarDemoDefaultAxis() {
        return barDemoDefaultAxis;
    }

    public void setBarDemoDefaultAxis(Axis barDemoDefaultAxis) {
        this.barDemoDefaultAxis = barDemoDefaultAxis;
    }

      public Axis getBarDemoYAxis() {
        return barDemoYAxis;
    }

    public void setBarDemoYAxis(Axis barDemoYAxis) {
        this.barDemoYAxis = barDemoYAxis;
    }
   
    public FactReportBean() {
        this.type="csv";
        
    }
    private Random randomizer = new Random(System.currentTimeMillis());
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
    
}
